﻿using System;
using Shuba.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace Shuba.Data
{
    public class SOTargetPlan
    {
        public SOTargetPlan()
        {
        }

        public void SaveSOTargetPlan(SOTargetPlanInfo TBL_SO_TARGET_PLAN)
        {
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand(@"
                                UPDATE TBL_SO_TARGET_PLAN_TMP 
                                SET [TARGET_AMOUNT] = @TARGET_AMOUNT
                                    ,[NOA] = @NOA
                                    ,[TARGET_AMOUNT_REAL] = @TARGET_AMOUNT_REAL
                                    ,[NOA_REAL] = @NOA_REAL
                                    ,[ENTRY_BY] = @ENTRY_BY
                                    ,[ENTRY_DATE] = @ENTRY_DATE
                                    ,[UPDATE_BY] = @UPDATE_BY
                                    ,[UPDATE_DATE] = @UPDATE_DATE
                                    ,[IS_DELETED] = @IS_DELETED
                                WHERE (TARGET_YEAR = @TARGET_YEAR) AND (TARGET_MONTH = @TARGET_MONTH) AND (AO_CODE = @AO_CODE) AND (UNIT_CODE = @UNIT_CODE);
                                IF @@ROWCOUNT = 0
                                INSERT INTO TBL_SO_TARGET_PLAN_TMP 
                                (
                                       [TARGET_YEAR]
                                      ,[TARGET_MONTH]
                                      ,[AO_CODE]
                                      ,[UNIT_CODE]
                                      ,[TARGET_AMOUNT]
                                      ,[NOA]
                                      ,[TARGET_AMOUNT_REAL]
                                      ,[NOA_REAL]
                                      ,[ENTRY_BY]
                                      ,[ENTRY_DATE]
                                      ,[UPDATE_BY]
                                      ,[UPDATE_DATE]
                                      ,[IS_DELETED]
                                ) 
                                VALUES
                                (
                                       LEFT(@TARGET_YEAR,4)
                                      ,LEFT(@TARGET_MONTH,2)
                                      ,@AO_CODE
                                      ,@UNIT_CODE
                                      ,@TARGET_AMOUNT
                                      ,@NOA
                                      ,@TARGET_AMOUNT_REAL
                                      ,@NOA_REAL
                                      ,@ENTRY_BY
                                      ,@ENTRY_DATE
                                      ,@UPDATE_BY
                                      ,@UPDATE_DATE
                                      ,@IS_DELETED
                                );
                            ");
            db.AddInParameter(cmd, "@TARGET_YEAR", DbType.String, TBL_SO_TARGET_PLAN.TARGET_YEAR);
            db.AddInParameter(cmd, "@TARGET_MONTH", DbType.String, TBL_SO_TARGET_PLAN.TARGET_MONTH);
            db.AddInParameter(cmd, "@AO_CODE", DbType.String, TBL_SO_TARGET_PLAN.AO_CODE);
            db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, TBL_SO_TARGET_PLAN.UNIT_CODE);
            db.AddInParameter(cmd, "@TARGET_AMOUNT", DbType.Double, TBL_SO_TARGET_PLAN.TARGET_AMOUNT);
            db.AddInParameter(cmd, "@NOA", DbType.Int32, TBL_SO_TARGET_PLAN.NOA);
            db.AddInParameter(cmd, "@TARGET_AMOUNT_REAL", DbType.Double, TBL_SO_TARGET_PLAN.TARGET_AMOUNT_REAL);
            db.AddInParameter(cmd, "@NOA_REAL", DbType.Int32, TBL_SO_TARGET_PLAN.NOA_REAL);
            db.AddInParameter(cmd, "@ENTRY_BY", DbType.String, TBL_SO_TARGET_PLAN.ENTRY_BY);
            db.AddInParameter(cmd, "@ENTRY_DATE", DbType.DateTime, TBL_SO_TARGET_PLAN.ENTRY_DATE);
            db.AddInParameter(cmd, "@UPDATE_BY", DbType.String, TBL_SO_TARGET_PLAN.UPDATE_BY);
            db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, TBL_SO_TARGET_PLAN.UPDATE_DATE);
            db.AddInParameter(cmd, "@IS_DELETED", DbType.String, TBL_SO_TARGET_PLAN.IS_DELETED);
            db.ExecuteNonQuery(cmd);
        }

        /*
        public void SaveSOTargetPlan(SOTargetPlanInfo TBL_SO_TARGET_PLAN)
        {
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand(@"
                                UPDATE TBL_SO_TARGET_PLAN_TMP 
                                SET [TARGET_AMOUNT] = @TARGET_AMOUNT
                                    ,[NOA] = @NOA
                                    ,[TARGET_AMOUNT_REAL] = @TARGET_AMOUNT_REAL
                                    ,[NOA_REAL] = @NOA_REAL
                                    ,[ENTRY_BY] = @ENTRY_BY
                                    ,[ENTRY_DATE] = @ENTRY_DATE
                                    ,[UPDATE_BY] = @UPDATE_BY
                                    ,[UPDATE_DATE] = @UPDATE_DATE
                                    ,[IS_DELETED] = @IS_DELETED
                                WHERE (TARGET_YEAR = @TARGET_YEAR) AND (TARGET_MONTH = @TARGET_MONTH) AND (AO_CODE = @AO_CODE) AND (UNIT_CODE = @UNIT_CODE);
                                IF @@ROWCOUNT = 0
                                INSERT INTO TBL_SO_TARGET_PLAN_TMP
                                (
                                       [TARGET_YEAR]
                                      ,[TARGET_MONTH]
                                      ,[AO_CODE]
                                      ,[UNIT_CODE]
                                      ,[TARGET_AMOUNT]
                                      ,[NOA]
                                      ,[TARGET_AMOUNT_REAL]
                                      ,[NOA_REAL]
                                      ,[ENTRY_BY]
                                      ,[ENTRY_DATE]
                                      ,[UPDATE_BY]
                                      ,[UPDATE_DATE]
                                      ,[IS_DELETED]
                                ) 
                                VALUES
                                (
                                       LEFT(@TARGET_YEAR,4)
                                      ,LEFT(@TARGET_MONTH,2)
                                      ,@AO_CODE
                                      ,@UNIT_CODE
                                      ,@TARGET_AMOUNT
                                      ,@NOA
                                      ,@TARGET_AMOUNT_REAL
                                      ,@NOA_REAL
                                      ,@ENTRY_BY
                                      ,@ENTRY_DATE
                                      ,@UPDATE_BY
                                      ,@UPDATE_DATE
                                      ,@IS_DELETED
                                );
                            ");
            db.AddInParameter(cmd, "@TARGET_YEAR", DbType.String, TBL_SO_TARGET_PLAN.TARGET_YEAR);
            db.AddInParameter(cmd, "@TARGET_MONTH", DbType.String, TBL_SO_TARGET_PLAN.TARGET_MONTH);
            db.AddInParameter(cmd, "@AO_CODE", DbType.String, TBL_SO_TARGET_PLAN.AO_CODE);
            db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, TBL_SO_TARGET_PLAN.UNIT_CODE);
            db.AddInParameter(cmd, "@TARGET_AMOUNT", DbType.Double, TBL_SO_TARGET_PLAN.TARGET_AMOUNT);
            db.AddInParameter(cmd, "@NOA", DbType.Int32, TBL_SO_TARGET_PLAN.NOA);
            db.AddInParameter(cmd, "@TARGET_AMOUNT_REAL", DbType.Double, TBL_SO_TARGET_PLAN.TARGET_AMOUNT_REAL);
            db.AddInParameter(cmd, "@NOA_REAL", DbType.Int32, TBL_SO_TARGET_PLAN.NOA_REAL);
            db.AddInParameter(cmd, "@ENTRY_BY", DbType.String, TBL_SO_TARGET_PLAN.ENTRY_BY);
            db.AddInParameter(cmd, "@ENTRY_DATE", DbType.DateTime, TBL_SO_TARGET_PLAN.ENTRY_DATE);
            db.AddInParameter(cmd, "@UPDATE_BY", DbType.String, TBL_SO_TARGET_PLAN.UPDATE_BY);
            db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, TBL_SO_TARGET_PLAN.UPDATE_DATE);
            db.AddInParameter(cmd, "@IS_DELETED", DbType.String, TBL_SO_TARGET_PLAN.IS_DELETED);
            db.ExecuteNonQuery(cmd);
        }
         */
    }
}
